package com.dy.nygl.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.DeviceLog;
import com.dy.nygl.service.DeviceLogService;
import com.dy.nygl.service.DeviceService;

@Controller
@RequestMapping("/log")
public class DeviceLogController {
	
	@Resource(name="deviceLogService")
	private DeviceLogService deviceLogService;
	@Resource(name="deviceService")
	private DeviceService deviceService;
	
	
	/**
	 * getDeviceError 设备效率损失时间柱形图显示类
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getDeviceError")
	@ResponseBody
	public Map<String,Object> getDeviceError(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date begin = sdf.parse(request.getParameter("begintime"));
		Date end = sdf.parse(request.getParameter("endtime"));		
				
		List<Integer> diList = deviceLogService.getDeviceIdByGroup(begin, end);
		Map<String,Object> map = new LinkedHashMap<String,Object>();
		if(diList !=null && diList.size() > 0) {
			List<DeviceLog> dlList = deviceLogService.getDeviceLogError(begin, end);
			List<Double> hourList = new ArrayList<Double>();
			//将同一编号的设备故障时间进行相加
			for(int i=0; i<diList.size(); i++) {
				double hour = 0;
				for(int j=0; j<dlList.size(); j++) {
					if(diList.get(i) == dlList.get(j).getDeviceId()) {
						//设备故障开始时间在begin之前
						if(dlList.get(j).getStarttime().before(begin)) {
							long m = dlList.get(j).getEndtime().getTime() - begin.getTime();
							hour += (double)m / (1000 * 60 * 60);
						} else if(begin.before(dlList.get(j).getStarttime()) && end.after(dlList.get(j).getEndtime())){
							long m = dlList.get(j).getEndtime().getTime() - dlList.get(j).getStarttime().getTime();
							hour += (double)m / (1000 * 60 * 60);
						} else if(dlList.get(j).getEndtime().equals(dlList.get(j).getStarttime())) {  //设备故障时间结束时间在end之后，不等式的写法和endtime的默认值有关
							long m = end.getTime() - dlList.get(j).getStarttime().getTime();
							hour += (double)m / (1000 * 60 * 60);
						} 
					}
				}
				hourList.add((double)(Math.round(hour * 100)) / 100.0);   //Math.round() 取整
			}
			List<String> dnList = new ArrayList<String>();
			for(int i=0; i<diList.size(); i++) {
				dnList.add(deviceService.getDeviceById(diList.get(i)).getNumber());
			}
			
			map.put("deviceNum", dnList);
			map.put("loseHour", hourList);
			
			System.out.println(map);
			
		}
		
		return map;
	}
	
	/**
	 * getDeviceLog 返回设备故障日志类
	 * @param request
	 * @param respone
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/getDeviceLogError")
	public ModelAndView getDeviceLogError(HttpServletRequest request, HttpServletResponse respone) throws Exception {
		
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 5;
		int totalRows=deviceLogService.getDeviceLogErrorCount(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			
			List<DeviceLog> dllist= deviceLogService.getDeviceLogErrorByPages(begin, rowsPage); 
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			try {
				for(int i=0; i<dllist.size(); i++) {
					Map<String, Object> map = new LinkedHashMap <String, Object>();
					double hour = 0.0;
					map.put("id", dllist.get(i).getId());
					map.put("num", deviceService.getDeviceById(dllist.get(i).getDeviceId()).getNumber());
					map.put("begintime", dllist.get(i).getStarttime());
					if(dllist.get(i).getEndtime().equals(dllist.get(i).getStarttime())) {
						Timestamp date = new Timestamp(System.currentTimeMillis());
//						SimpleDateFormat sdf = new SimpleDateFormat("");
						map.put("endtime", date);
						hour += date.getTime() - dllist.get(i).getStarttime().getTime();
					} else {
						map.put("endtime", dllist.get(i).getEndtime());
						hour += dllist.get(i).getEndtime().getTime() - dllist.get(i).getStarttime().getTime();
					}
					hour = (double)hour / (1000 * 60 * 60);
					hour = (double)(Math.round(hour * 100)) / 100.0;
					map.put("hour", hour);
					list.add(map);					
				}	
			} catch(Exception e) {
				list = null;
			}
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		
		model.setViewName("device/device_log");
		return model;
	}
	
	@RequestMapping("/getDeviceEffect")
	public ModelAndView getDeviceEffect(HttpServletRequest request) throws Exception {
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 5;
		
		List<Integer> diList = deviceLogService.getDeviceIdOnGroup();
		int totalRows=diList.size(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			int end=currPage*rowsPage - 1;
			List<Map<String, Object>> dlList= deviceLogService.getDeviceLogOnEffectByPages(begin, end); 
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			try {
				for(int i=0; i<dlList.size(); i++) {
					Map<String, Object> map = new LinkedHashMap <String, Object>();
					map.put("deviceNum", dlList.get(i).get("deviceNum"));
					map.put("worktime", dlList.get(i).get("worktime"));
					map.put("errortime", dlList.get(i).get("errortime"));
					map.put("effect", dlList.get(i).get("effect"));
					list.add(map);					
				}	
			} catch(Exception e) {
				list = null;
			}
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		model.setViewName("device/device_effect");
		return model;
	}
	
	@RequestMapping("/selectByNum")
	public ModelAndView selectByNum(HttpServletRequest request) throws Exception {
		
		String deviceNum = request.getParameter("keyword");
		Map<String, Object> map = deviceLogService.getDeviceLogOnEffectByDeviceNum(deviceNum);
		
		ModelAndView model = new ModelAndView();
		
		model.addObject("map",map);
		model.setViewName("device/device_select");
		return model;
	}
	
	@RequestMapping("/getDeviceHighEffect")
	public ModelAndView getDeviceHighEffect(HttpServletRequest request) throws Exception {
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 5;
		int totalRows=deviceLogService.getDeviceLogOnHighEffectList().size(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			int end = currPage*rowsPage - 1;
			List<Map<String, Object>> list= deviceLogService.getDeviceLogOnHighEffect(begin, end); 
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		
		model.setViewName("device/device_higheffect");
		return model;
	}
	
	@RequestMapping("/getDeviceLowEffect")
	public ModelAndView getDeviceLowEffect(HttpServletRequest request) throws Exception {
		ModelAndView model = new ModelAndView();
		int currPage = 1;
		int rowsPage = 5;
		int totalRows=deviceLogService.getDeviceLogOnLowEffectList().size(); //总行数
		
		int totalPage=totalRows/rowsPage;//总页数
		totalPage=(totalRows%rowsPage)>0?totalPage+1:totalPage;
		
		if(totalPage != 0) {
			if (request.getParameter("currPage")!=null) {
				try { 
					currPage=Integer.parseInt(request.getParameter("currPage"));//当前显示第几页
					if(currPage < 1) {
						currPage = 1;
					} else if(currPage > totalPage) {
						currPage = totalPage;
					}
				} catch(Exception e) { 
					currPage=1;//当前显示第几页
				}
			} else {
				currPage=1;//当前显示第几页
			}		
			
			int begin=(currPage-1)*rowsPage;//计算分页开始行
			int end = currPage*rowsPage;
			List<Map<String, Object>> list= deviceLogService.getDeviceLogOnLowEffect(begin, end); 
			model.addObject("currPage", currPage);
			model.addObject("rowsPage", rowsPage);
			model.addObject("totalRows", totalRows);
			model.addObject("totalPage", totalPage);
			model.addObject("list", list);
		}
		
		model.setViewName("device/device_loweffect");
		return model;
	}
}
